﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Text;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Assist;
using YArchitech.Controls;
using YArchitech.LIB;
using YJKRGeometry;
using YJKTextDimension.Utility;

namespace YJKDimensionModel.ArrowDimension
{
	public partial class DlgArrowDimensionForm : System.Windows.Forms.Form
	{
		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		internal ArrowDimData UseData
		{
			get
			{
				return this.m_useData;
			}
			private set
			{
				this.m_useData = value;
			}
		}

		public DlgArrowDimensionForm()
		{
			this.InitializeComponent();
			this.InitTabMap();
		}

		private void DlgArrowDimension_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			new List<string>();
			InstalledFontCollection installedFontCollection = new InstalledFontCollection();
			FontFamily[] families = installedFontCollection.Families;
			if (families == null || families.Length < 1)
			{
				return;
			}
			foreach (FontFamily fontFamily in families)
			{
				this.cboTextStyle.Items.Add(fontFamily.Name);
			}
			this.cboTextStyle.SelectedItem = "仿宋";
			Helper.AddCombItems(this.lstArrowStyle, this.cboArrowStyle);
			Helper.AddCombItems(this.lstAlignStyle, this.cboAlignStyle);
			Helper.AddCombItems(this.lstDistance, this.cboDistance);
			Helper.AddCombItems(this.lstTextheight, this.cboTextHeight);
			foreach (string item in this.lstArrowAngle)
			{
				this.cboArrowAngle.Items.Add(item);
			}
			this.cboArrowAngle.SelectedIndex = 1;
			this.txtTextWidth.Text = "0.7";
			if (this.cboArrowStyle.Items.Count > 0)
			{
				this.cboArrowStyle.SelectedIndex = 0;
			}
			if (this.cboTextHeight.Items.Count > 0)
			{
				this.cboTextHeight.SelectedIndex = 0;
			}
			if (this.cboAlignStyle.Items.Count > 0)
			{
				this.cboAlignStyle.SelectedIndex = 0;
			}
			if (this.cboDistance.Items.Count > 0)
			{
				this.cboDistance.SelectedIndex = 0;
			}
			this.ReadDlgData();
		}

		private bool IsNumeric(string str)
		{
			double val;
			return double.TryParse(str, out val) && !Geometry.LessThan(val, 0.0);
		}

		private bool IsInteger(string str)
		{
			int num;
			return int.TryParse(str, out num) && !Geometry.LessThan((double)num, 0.0);
		}

		private void SaveDlgData()
		{
			string appRootPath = AssistFunc.GetAppRootPath();
			string str = appRootPath + "\\Data\\";
			string path = str + "DlgArrowDimension.xml";
			FileStream fileStream = new FileStream(path, FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(this.txtUpLine.Text);
			streamWriter.WriteLine(this.txtDownLine.Text);
			streamWriter.WriteLine(this.cboTextStyle.SelectedIndex.ToString());
			streamWriter.WriteLine(this.cboArrowStyle.SelectedIndex.ToString());
			streamWriter.WriteLine(this.cboTextHeight.Text);
			streamWriter.WriteLine(this.cboArrowAngle.Text);
			streamWriter.WriteLine(this.txtTextWidth.Text);
			streamWriter.WriteLine(this.cboAlignStyle.SelectedIndex.ToString());
			streamWriter.WriteLine(this.cboDistance.Text);
			streamWriter.WriteLine(base.Location.X);
			streamWriter.WriteLine(base.Location.Y);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private bool ReadDlgData()
		{
			string appRootPath = AssistFunc.GetAppRootPath();
			string str = appRootPath + "\\Data\\";
			string path = str + "DlgArrowDimension.xml";
			if (!File.Exists(path))
			{
				base.StartPosition = FormStartPosition.CenterScreen;
				return true;
			}
			base.StartPosition = FormStartPosition.Manual;
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			this.txtUpLine.Text = streamReader.ReadLine();
			this.txtDownLine.Text = streamReader.ReadLine();
			string text = streamReader.ReadLine();
			if (this.cboTextStyle.Items.Count > Convert.ToInt32(text))
			{
				this.cboTextStyle.SelectedIndex = Convert.ToInt32(text);
			}
			text = streamReader.ReadLine();
			if (this.cboArrowStyle.Items.Count > Convert.ToInt32(text))
			{
				this.cboArrowStyle.SelectedIndex = Convert.ToInt32(text);
			}
			text = streamReader.ReadLine();
			this.cboTextHeight.Text = text;
			text = streamReader.ReadLine();
			this.cboArrowAngle.Text = text;
			text = streamReader.ReadLine();
			this.txtTextWidth.Text = text;
			text = streamReader.ReadLine();
			if (this.cboAlignStyle.Items.Count > Convert.ToInt32(text))
			{
				this.cboAlignStyle.SelectedIndex = Convert.ToInt32(text);
			}
			text = streamReader.ReadLine();
			this.cboDistance.Text = text;
			try
			{
				int x = Convert.ToInt32(streamReader.ReadLine());
				int y = Convert.ToInt32(streamReader.ReadLine());
				base.Location = new System.Drawing.Point(x, y);
			}
			catch (Exception)
			{
				streamReader.Close();
				fileStream.Close();
				return true;
			}
			streamReader.Close();
			fileStream.Close();
			return true;
		}

		protected override bool ProcessKeyEventArgs(ref Message m)
		{
			int num = 256;
			int num2 = 27;
			if (m.Msg == num)
			{
				if (m.WParam.ToInt32() == num2)
				{
					base.Close();
				}
				if (m.WParam.ToInt32() == 9)
				{
					Control activeControl = base.ActiveControl;
					if (activeControl != null && this.tabMap.Keys.Contains(activeControl.Name))
					{
						Control[] array = base.Controls.Find(this.tabMap[activeControl.Name], true);
						if (array.Length > 0)
						{
							this.ActivieParentContainerIfNeeded(array[0]);
							array[0].Focus();
							array[0].CausesValidation = true;
							if (array[0].Focused)
							{
								return base.ProcessKeyEventArgs(ref m);
							}
						}
					}
				}
			}
			return base.ProcessKeyEventArgs(ref m);
		}

		private void txtUpLine_TextChanged(object sender, EventArgs e)
		{
			this.m_useData.StrLineUpText = this.txtUpLine.Text;
		}

		private void txtDownLine_TextChanged(object sender, EventArgs e)
		{
			this.m_useData.StrLineDownText = this.txtDownLine.Text;
		}

		private void cboTextStyle_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.cboTextStyle.Items.Count > 0)
			{
				this.m_useData.StrFontType = this.cboTextStyle.SelectedItem.ToString();
			}
		}

		private void cboArrowStyle_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.m_useData.StrArrowType = this.cboArrowStyle.SelectedItem.ToString();
		}

		private void cboTextHeight_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (string.IsNullOrEmpty(this.cboTextHeight.Text))
			{
				this.m_useData.StrTextHeight = string.Empty;
				this.cboTextHeight.Focus();
				this.m_useData.BRight = false;
				return;
			}
			if (this.IsNumeric(this.cboTextHeight.Text))
			{
				this.m_useData.BRight = true;
				this.m_useData.StrTextHeight = this.cboTextHeight.Text;
				return;
			}
			this.cboTextHeight.Focus();
			this.m_useData.BRight = false;
			this.m_useData.StrTextHeight = string.Empty;
			YJKMessageBox.Show("文字高度应为正数！");
		}

		private void cboArrowAngle_SelectedIndexChanged(object sender, EventArgs e)
		{
			string text = this.cboArrowAngle.SelectedItem.ToString();
			if (text.Contains("°"))
			{
				text = text.Substring(0, text.Length - 1);
			}
			if (this.cboArrowStyle.SelectedItem.ToString().Equals("单侧实心箭头") || this.cboArrowStyle.SelectedItem.ToString().Equals("双侧实心箭头"))
			{
				if (this.IsInteger(text) && Convert.ToDouble(text) < 61.0 && Convert.ToDouble(text) > 14.0)
				{
					this.m_useData.BRight = true;
					this.m_useData.StrArrowAngle = this.cboArrowAngle.SelectedItem.ToString();
					return;
				}
				this.cboArrowAngle.Focus();
				this.m_useData.BRight = false;
				YJKMessageBox.Show("实心箭头开合角度为15~60之间的正整数。");
				this.cboArrowAngle.SelectedIndex = 1;
				return;
			}
			else
			{
				if (this.IsInteger(text) && Convert.ToDouble(text) < 61.0 && Convert.ToDouble(text) > 9.0)
				{
					this.m_useData.BRight = true;
					this.m_useData.StrArrowAngle = this.cboArrowAngle.Text;
					return;
				}
				this.cboArrowAngle.Focus();
				this.m_useData.BRight = false;
				this.m_useData.StrArrowAngle = string.Empty;
				this.cboArrowAngle.SelectedIndex = 1;
				YJKMessageBox.Show("箭头开合角度为10~60之间的正整数。");
				return;
			}
		}

		private void cboAlignStyle_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.cboAlignStyle.Items.Count > 0)
			{
				this.m_useData.StrAlignStyle = this.cboAlignStyle.Text;
				if (this.m_useData.StrAlignStyle == "在线端")
				{
					this.txtDownLine.Enabled = false;
					return;
				}
				this.txtDownLine.Enabled = true;
			}
		}

		private void cboDistance_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (string.IsNullOrEmpty(this.cboDistance.Text))
			{
				this.m_useData.StrDistance = string.Empty;
				this.m_useData.BRight = false;
				return;
			}
			if (this.IsInteger(this.cboDistance.SelectedItem.ToString()))
			{
				this.m_useData.StrDistance = this.cboDistance.SelectedItem.ToString();
				this.m_useData.BRight = true;
				return;
			}
			this.m_useData.BRight = false;
			this.m_useData.StrDistance = string.Empty;
			this.cboDistance.SelectedIndex = 1;
			YJKMessageBox.Show("文字与基线的距离为正整数。");
		}

		private void txtTextWidth_TextChanged(object sender, EventArgs e)
		{
			if (string.IsNullOrEmpty(this.txtTextWidth.Text))
			{
				this.m_useData.StrTextWidth = string.Empty;
				this.m_useData.BRight = false;
				return;
			}
			if (this.IsNumeric(this.txtTextWidth.Text))
			{
				this.m_useData.StrTextWidth = this.txtTextWidth.Text;
				this.m_useData.BRight = true;
				return;
			}
			this.txtTextWidth.Text = "0.7";
			this.m_useData.BRight = false;
			this.m_useData.StrTextWidth = string.Empty;
			YJKMessageBox.Show("字宽为正数，请重新输入！");
			this.txtTextWidth.Focus();
		}

		private void SendEscapeMessage()
		{
			int wMsg = 7;
			DlgArrowDimensionForm.SendMessage(CmdArrowDimension._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			byte bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			DlgArrowDimensionForm.keybd_event(bVk, 0, dwFlags, 0);
			DlgArrowDimensionForm.keybd_event(bVk, 0, dwFlags2, 0);
		}

		private void DlgArrowDimension_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.SaveDlgData();
		}

		private void DlgArrowDimension_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(base.PointToClient(Cursor.Position)))
			{
				this.CheckMethod();
				int wMsg = 7;
				DlgArrowDimensionForm.SendMessage(CmdArrowDimension._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			}
			else
			{
				base.Focus();
			}
			try
			{
				this.m_useData.StrAlignStyle = this.cboAlignStyle.SelectedItem.ToString();
				this.m_useData.StrLineUpText = this.txtUpLine.Text.Trim();
				this.m_useData.StrLineDownText = this.txtDownLine.Text.Trim();
				this.m_useData.StrFontType = this.cboTextStyle.SelectedItem.ToString();
				this.m_useData.StrArrowType = this.cboArrowStyle.SelectedItem.ToString();
				this.m_useData.StrArrowAngle = this.cboArrowAngle.Text.Trim();
				this.m_useData.StrTextWidth = this.txtTextWidth.Text.Trim();
				this.m_useData.StrDistance = this.cboDistance.Text.Trim();
			}
			catch (Exception)
			{
			}
		}

		private void cboTextHeight_TextChanged(object sender, EventArgs e)
		{
			if (!string.IsNullOrEmpty(this.cboTextHeight.Text))
			{
				if (this.IsNumeric(this.cboTextHeight.Text))
				{
					this.m_useData.StrTextHeight = this.cboTextHeight.Text;
					this.m_useData.BRight = true;
					return;
				}
				this.cboTextHeight.Focus();
				this.m_useData.StrTextHeight = string.Empty;
				this.cboTextHeight.SelectedIndex = 0;
				this.m_useData.BRight = false;
				YJKMessageBox.Show("文字高度应为正数！");
			}
		}

		private void cboArrowAngle_TextChanged(object sender, EventArgs e)
		{
		}

		private void cboDistance_TextChanged(object sender, EventArgs e)
		{
			if (!string.IsNullOrEmpty(this.cboDistance.Text))
			{
				if (this.IsInteger(this.cboDistance.Text))
				{
					this.m_useData.StrDistance = this.cboDistance.Text;
					this.m_useData.BRight = true;
					return;
				}
				this.m_useData.BRight = false;
				this.cboDistance.SelectedIndex = 1;
				YJKMessageBox.Show("文字与基线的距离为正整数。");
			}
		}

		private void cboArrowAngle_Leave(object sender, EventArgs e)
		{
			string text = this.cboArrowAngle.Text;
			if (text.Contains("°"))
			{
				text = text.Substring(0, text.Length - 1);
			}
			if (this.cboArrowStyle.SelectedItem.ToString().Equals("单侧实心箭头") || this.cboArrowStyle.SelectedItem.ToString().Equals("双侧实心箭头"))
			{
				if (this.IsInteger(text) && Convert.ToDouble(text) < 61.0 && Convert.ToDouble(text) > 14.0)
				{
					this.m_useData.StrArrowAngle = this.cboArrowAngle.SelectedItem.ToString();
					this.m_useData.BRight = true;
					return;
				}
				this.cboArrowAngle.Focus();
				this.m_useData.BRight = false;
				this.m_useData.StrArrowAngle = string.Empty;
				YJKMessageBox.Show("实心箭头开合角度为15~60之间的正整数。");
				this.cboArrowAngle.SelectedIndex = 1;
				return;
			}
			else
			{
				if (this.IsInteger(text) && Convert.ToDouble(text) < 61.0 && Convert.ToDouble(text) > 9.0)
				{
					this.m_useData.StrArrowAngle = this.cboArrowAngle.Text;
					this.m_useData.BRight = true;
					return;
				}
				this.cboArrowAngle.Focus();
				this.m_useData.StrArrowAngle = string.Empty;
				this.cboArrowAngle.SelectedIndex = 1;
				this.m_useData.BRight = false;
				YJKMessageBox.Show("箭头开合角度为10~60之间的正整数。");
				return;
			}
		}

		private void DlgArrowDimensionForm_Leave(object sender, EventArgs e)
		{
			this.CheckMethod();
		}

		private void CheckMethod()
		{
			string text = this.cboArrowAngle.Text;
			if (text.Contains("°"))
			{
				text = text.Substring(0, text.Length - 1);
			}
			if (this.cboArrowStyle.SelectedItem.ToString().Equals("单侧实心箭头") || this.cboArrowStyle.SelectedItem.ToString().Equals("双侧实心箭头"))
			{
				if (this.IsInteger(text) && Convert.ToDouble(text) < 61.0 && Convert.ToDouble(text) > 14.0)
				{
					this.m_useData.StrArrowAngle = this.cboArrowAngle.Text.ToString();
					this.m_useData.BRight = true;
					return;
				}
				this.cboArrowAngle.Focus();
				this.m_useData.BRight = false;
				this.m_useData.StrArrowAngle = string.Empty;
				YJKMessageBox.Show("实心箭头开合角度为15~60之间的正整数。");
				this.cboArrowAngle.SelectedIndex = 1;
				return;
			}
			else
			{
				if (this.IsInteger(text) && Convert.ToDouble(text) < 61.0 && Convert.ToDouble(text) > 9.0)
				{
					this.m_useData.StrArrowAngle = this.cboArrowAngle.Text;
					this.m_useData.BRight = true;
					return;
				}
				this.cboArrowAngle.Focus();
				this.m_useData.StrArrowAngle = string.Empty;
				this.cboArrowAngle.SelectedIndex = 1;
				this.m_useData.BRight = false;
				YJKMessageBox.Show("箭头开合角度为10~60之间的正整数。");
				return;
			}
		}

		private void InitTabMap()
		{
			this.tabMap.Add("txtUpLine", "txtDownLine");
			this.tabMap.Add("txtDownLine", "cboTextStyle");
			this.tabMap.Add("cboTextStyle", "cboArrowStyle");
			this.tabMap.Add("cboArrowStyle", "cboTextHeight");
			this.tabMap.Add("cboTextHeight", "cboArrowAngle");
			this.tabMap.Add("cboArrowAngle", "txtTextWidth");
			this.tabMap.Add("txtTextWidth", "cboAlignStyle");
			this.tabMap.Add("cboAlignStyle", "cboDistance");
			this.tabMap.Add("cboDistance", "txtUpLine");
		}

		private void ActivieParentContainerIfNeeded(Control child)
		{
			if (child.Visible)
			{
				return;
			}
			Control parent = child.Parent;
			while (parent != null && !(parent is TabPage))
			{
				parent = parent.Parent;
			}
			if (parent is TabPage)
			{
				TabControl tabControl = (TabControl)parent.Parent;
				tabControl.SelectedTab = (parent as TabPage);
			}
		}

		private ArrowDimData m_useData = new ArrowDimData();

		private List<string> lstAlignStyle = new List<string>
		{
			"齐线端",
			"齐线中",
			"在线端"
		};

		private List<string> lstTextheight = new List<string>
		{
			"3.0",
			"3.5",
			"5.0",
			"6.0",
			"7.0"
		};

		private List<string> lstDistance = new List<string>
		{
			"1",
			"2",
			"3",
			"4",
			"5"
		};

		private List<string> lstArrowStyle = new List<string>
		{
			"双侧实心箭头",
			"双侧空心箭头",
			"单侧实心箭头",
			"单侧空心箭头",
			"单线箭头",
			"双线箭头"
		};

		private List<string> lstArrowAngle = new List<string>
		{
			"10°",
			"15°",
			"30°",
			"45°",
			"60°"
		};

		private Dictionary<string, string> tabMap = new Dictionary<string, string>();
	}
}
